package com.abe.bathsystem.android;

import android.content.Intent;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;
import android.widget.TextView;
import android.widget.Toast;

import com.abe.bathsystem.android.service.FlowWindowService;
import com.abe.bathsystem.base.AutoActivity;
import com.abe.bathsystem.utils.AudioUtil;

import net.yet.campusdev.R;

import butterknife.BindView;
import butterknife.OnClick;


public class SoundActivity extends AutoActivity {
    @BindView(R.id.item_value)
    TextView itemValue;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_v3_sound;
    }

    // SoundPool对象
    private SoundPool mSound;
    private int[] mSoundID;


    @Override
    protected void initContent() {
        initSoundPool();
        updateTxt();
    }

    public void initSoundPool() {
        mSoundID = new int[16];
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mSound = new SoundPool.Builder()
                    .setMaxStreams(1)
                    .build();
        } else {
            mSound = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
        }
        initMusic();
    }

    /**
     * 初始化音频文件
     */
    private void initMusic() {
        //分别加入到SoundPool中
        mSoundID[0] = mSound.load(this, R.raw.startbath01, 1);// 1
        mSoundID[1] = mSound.load(this, R.raw.startbath02, 1);// 2
        mSoundID[2] = mSound.load(this, R.raw.startbath03, 1);// 3
        mSoundID[3] = mSound.load(this, R.raw.startbath04, 1);// 4
        mSoundID[4] = mSound.load(this, R.raw.startbath05, 1);// 5
        mSoundID[5] = mSound.load(this, R.raw.startbath06, 1);// 6
        mSoundID[6] = mSound.load(this, R.raw.startbath07, 1);// 7
        mSoundID[7] = mSound.load(this, R.raw.startbath08, 1);// 8
        mSoundID[8] = mSound.load(this, R.raw.startbath09, 1);// 9
        mSoundID[9] = mSound.load(this, R.raw.startbath10, 1);// 10
        mSoundID[10] = mSound.load(this, R.raw.startbath11, 1);// 11
        mSoundID[11] = mSound.load(this, R.raw.startbath12, 1);// 12
        mSoundID[12] = mSound.load(this, R.raw.startbath13, 1);// 13
        mSoundID[13] = mSound.load(this, R.raw.startbath14, 1);// 14
        mSoundID[14] = mSound.load(this, R.raw.startbath15, 1);// 15
        mSoundID[15] = mSound.load(this, R.raw.endbath, 1);// 16
    }

    /**
     * 播放MP3资源
     *
     * @param resId 资源ID
     */
    protected void StartMusic(int resId) {
        /*
         * 第一个参数为播放音频ID
         * 第二个 第三个为音量
         * 第四个为优先级
         * 第五个为是否循环播放
         * 第六个设置播放速度
         * 返回值 不为0即代表成功
         */
        mSound.play(resId, 1, 1, 0, 0, 1);
    }

    private void updateTxt() {
        StringBuilder builder = new StringBuilder();
        builder.append("当前音量：\n");
        builder.append(AudioUtil.getInstance(this).getMediaVolume());
        builder.append("\n最大音量\n");
        builder.append(AudioUtil.getInstance(this).getMediaMaxVolume());
        itemValue.setText(builder.toString());
    }

    @OnClick(R.id.item_set_max)
    public void onItemSetMaxClicked() {
        AudioUtil.getInstance(this).setMediaVolume(AudioUtil.getInstance(this).getMediaMaxVolume());
    }

    @OnClick(R.id.item_set_min)
    public void onItemSetMinClicked() {
        AudioUtil.getInstance(this).setMediaVolume(AudioUtil.getInstance(this).getMediaMaxVolume() / 2);
    }

    @OnClick(R.id.item_play)
    public void onViewClicked() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                updateTxt();
                StartMusic(mSoundID[15]);
            }
        });
    }

    @OnClick(R.id.item_open_window)
    public void onOpenWin() {
        //startActivity(new Intent(Settings.ACTION_SETTINGS));
        Intent intent = new Intent(this, FlowWindowService.class);
        Toast.makeText(this, "已开启Toucher", Toast.LENGTH_SHORT).show();
        startService(intent);
    }


    @OnClick(R.id.item_send_anr)
    public void onSendANR() {
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
